package com.br.fsm.projectdelivery.repositorio;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.util.Log;

import com.br.fsm.projectdelivery.activity.R;
import com.br.fsm.projectdelivery.basica.Item;
import com.br.fsm.projectdelivery.basica.Item.Itens;
import com.br.fsm.projectdelivery.basica.Pedido;
import com.br.fsm.projectdelivery.basica.Pedido.Pedidos;
import com.br.fsm.projectdelivery.excecao.RepositorioException;
import com.br.fsm.projectdelivery.util.Constantes;
import com.br.fsm.projectdelivery.util.Util;

public class RepositorioItem extends RepositorioBasico implements IRepositorioItem {

	private static RepositorioItem instance;	
	
	public static final String TABLE_NAME = "ITEM";
	
	public static RepositorioItem getInstance(){
		if ( instance == null ){
			instance =  new RepositorioItem();
		} 
		return instance;
	}

	@Override
	public Item getItemById(int id) throws RepositorioException {
		Cursor cursor = null;
		
		Item item = null;
		
		try {
			cursor = db.query(TABLE_NAME, Item.getColumns(), Itens.ID + " = " + id , null,
					null, null, null, null);
			
			if (cursor.moveToFirst()) {
				
				item = new Item();
				
				int idxId = cursor.getColumnIndex(Itens.ID);
				int idxQtd = cursor.getColumnIndex(Itens.QTD);
				int idxPrecoUnitario = cursor.getColumnIndex(Itens.PRECOUNITARIO);
				int idxUltimaAlteracao = cursor.getColumnIndex(Itens.ULTIMAALTERACAO);
				
				item.setItemId(cursor.getInt(idxId));
				item.setQuantidade(cursor.getInt(idxQtd));
				item.setPrecoUnitario(cursor.getDouble(idxPrecoUnitario));
				item.setItemUltimaAlteracao(Util.convertDateStrToDate(cursor.getString(idxUltimaAlteracao)));
			}
		} catch (Exception e) {
			e.printStackTrace();
			Log.e( Constantes.CATEGORIA , e.getMessage());
			throw new RepositorioException(context.getResources().getString(
					R.string.db_error));
		} finally {
			if (cursor != null) {
				cursor.close();
			}
		}
		return item;
	}

	@Override
	public ArrayList<Item> getAllItem() throws RepositorioException {
		Cursor cursor = null;
		
		ArrayList<Item> itens = null;
		try {
			itens = new ArrayList<Item>();
			cursor = db.query(TABLE_NAME, Pedido.getColumns(), null , null,
					null, null, null, null);
			
			if (cursor.moveToFirst()) {
				
				int idxId = cursor.getColumnIndex(Itens.ID);
				int idxQtd = cursor.getColumnIndex(Itens.QTD);
				int idxPrecoUnitario = cursor.getColumnIndex(Itens.PRECOUNITARIO);
				int idxUltimaAlteracao = cursor.getColumnIndex(Pedidos.ULTIMAALTERACAO);
									
				do {
					Item item = new Item();
					
					item.setItemID(cursor.getInt(idxId));
					item.setQuantidade(cursor.getInt(idxQtd));
				    item.setPrecoUnitario(cursor.getDouble(idxPrecoUnitario));
					item.setItemUltimaAlteracao(Util.convertDateStrToDate(cursor.getString(idxUltimaAlteracao)));
					
					itens.add(item);
				} while (cursor.moveToNext());
				
			}
		} catch (Exception e) {
			e.printStackTrace();			
			Log.e( Constantes.CATEGORIA, e.getMessage());
			throw new RepositorioException(context.getResources().getString(
					R.string.db_error));
		} finally {
			if (cursor != null) {
				cursor.close();
			}
		}
		return itens;
	}

	@Override
	public void insertItem(Item item) throws RepositorioException {
		ContentValues values = new ContentValues();

		values.put(Itens.QTD, item.getQuantidade());
		values.put(Itens.PRECOUNITARIO, item.getPrecoUnitario());
		String data = Util.convertDateToDateStr(Util.getCurrentDateTime());
		values.put(Pedidos.ULTIMAALTERACAO, data);

		try {
			db.insert(TABLE_NAME, "", values);
		} catch (SQLException sqe) {
			sqe.printStackTrace();
			Log.e( Constantes.CATEGORIA , sqe.getMessage());
			throw new RepositorioException(context.getResources().getString(
					R.string.db_error));
		}		
	}

	@Override
	public void updateItem(Item item) throws RepositorioException {
		ContentValues values = new ContentValues();

		values.put(Itens.QTD, item.getQuantidade());
		values.put(Itens.PRECOUNITARIO, item.getPrecoUnitario());
		String data = Util.convertDateToDateStr(Util.getCurrentDateTime());
		values.put(Pedidos.ULTIMAALTERACAO, data);

		String _id = String.valueOf(item.getItemId());

		String where = Itens.ID + "=?";
		String[] whereArgs = new String[] { _id };

		try {
			db.update(TABLE_NAME, values, where, whereArgs);
		} catch (SQLException sqe) {
			sqe.printStackTrace();
			Log.e( Constantes.CATEGORIA , sqe.getMessage());
			throw new RepositorioException(context.getResources().getString(
					R.string.db_error));
		}
	}

	@Override
	public void removeItem(Item item) throws RepositorioException {
		String _id = String.valueOf(item.getItemId());

		String where = Itens.ID + "=?";
		String[] whereArgs = new String[] { _id };

		try {
			db.delete( TABLE_NAME, where, whereArgs);
		} catch (SQLException sqe) {
			sqe.printStackTrace();
			Log.e( Constantes.CATEGORIA , sqe.getMessage());
			throw new RepositorioException(context.getResources().getString(
					R.string.db_error));
		}
	}
}

